<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="language" content="en" />
        <link href="./assets/ffd55088/css/bootstrap.css" rel="stylesheet">
<link href="./assets/5cf9384a/solarized_light.css" rel="stylesheet">
<link href="./assets/6c54116e/style.css" rel="stylesheet">
<script src="./assets/a44cef0f/jquery.js"></script>
<script src="./assets/ffd55088/js/bootstrap.js"></script>
<script src="./assets/8ac4e28a/jssearch.js"></script>    <title>Handling Errors - Handling Requests - The Definitive Guide to Yii 2.0</title>
</head>
<body>

<div class="wrap">
    <nav id="w1289" class="navbar-inverse navbar-fixed-top navbar" role="navigation"><div class="navbar-header"><button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#w1289-collapse"><span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span></button><a class="navbar-brand" href="./index.html">The Definitive Guide to Yii 2.0</a></div><div id="w1289-collapse" class="collapse navbar-collapse"><ul id="w1290" class="navbar-nav nav"><li><a href="./index.html">Class reference</a></li>
<li class="dropdown"><a class="dropdown-toggle" href="#" data-toggle="dropdown">Extensions <b class="caret"></b></a><ul id="w1291" class="dropdown-menu"><li><a href="./ext-apidoc-index.html" tabindex="-1">apidoc</a></li>
<li><a href="./ext-authclient-index.html" tabindex="-1">authclient</a></li>
<li><a href="./ext-bootstrap-index.html" tabindex="-1">bootstrap</a></li>
<li><a href="./ext-codeception-index.html" tabindex="-1">codeception</a></li>
<li><a href="./ext-debug-index.html" tabindex="-1">debug</a></li>
<li><a href="./ext-elasticsearch-index.html" tabindex="-1">elasticsearch</a></li>
<li><a href="./ext-faker-index.html" tabindex="-1">faker</a></li>
<li><a href="./ext-gii-index.html" tabindex="-1">gii</a></li>
<li><a href="./ext-imagine-index.html" tabindex="-1">imagine</a></li>
<li><a href="./ext-jui-index.html" tabindex="-1">jui</a></li>
<li><a href="./ext-mongodb-index.html" tabindex="-1">mongodb</a></li>
<li><a href="./ext-redis-index.html" tabindex="-1">redis</a></li>
<li><a href="./ext-smarty-index.html" tabindex="-1">smarty</a></li>
<li><a href="./ext-sphinx-index.html" tabindex="-1">sphinx</a></li>
<li><a href="./ext-swiftmailer-index.html" tabindex="-1">swiftmailer</a></li>
<li><a href="./ext-twig-index.html" tabindex="-1">twig</a></li></ul></li>
<li><a href="./guide-README.html">Guide</a></li></ul><div class="navbar-form navbar-left" role="search">
  <div class="form-group">
    <input id="searchbox" type="text" class="form-control" placeholder="Search">
  </div>
</div>
</div></nav>
    <div id="search-resultbox" style="display: none;" class="modal-content">
        <ul id="search-results">
        </ul>
    </div>

    
<div class="row">
    <div class="col-md-2">
                <div id="navigation" class="list-group"><a class="list-group-item" href="#navigation-1273" data-toggle="collapse" data-parent="#navigation">Introduction <b class="caret"></b></a><div id="navigation-1273" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-intro-yii.html">About Yii</a>
<a class="list-group-item" href="./guide-intro-upgrade-from-v1.html">Upgrading from Version 1.1</a></div>
<a class="list-group-item" href="#navigation-1274" data-toggle="collapse" data-parent="#navigation">Getting Started <b class="caret"></b></a><div id="navigation-1274" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-start-installation.html">Installing Yii</a>
<a class="list-group-item" href="./guide-start-workflow.html">Running Applications</a>
<a class="list-group-item" href="./guide-start-hello.html">Saying Hello</a>
<a class="list-group-item" href="./guide-start-forms.html">Working with Forms</a>
<a class="list-group-item" href="./guide-start-databases.html">Working with Databases</a>
<a class="list-group-item" href="./guide-start-gii.html">Generating Code with Gii</a>
<a class="list-group-item" href="./guide-start-looking-ahead.html">Looking Ahead</a></div>
<a class="list-group-item" href="#navigation-1275" data-toggle="collapse" data-parent="#navigation">Application Structure <b class="caret"></b></a><div id="navigation-1275" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-structure-overview.html">Overview</a>
<a class="list-group-item" href="./guide-structure-entry-scripts.html">Entry Scripts</a>
<a class="list-group-item" href="./guide-structure-applications.html">Applications</a>
<a class="list-group-item" href="./guide-structure-application-components.html">Application Components</a>
<a class="list-group-item" href="./guide-structure-controllers.html">Controllers</a>
<a class="list-group-item" href="./guide-structure-models.html">Models</a>
<a class="list-group-item" href="./guide-structure-views.html">Views</a>
<a class="list-group-item" href="./guide-structure-modules.html">Modules</a>
<a class="list-group-item" href="./guide-structure-filters.html">Filters</a>
<a class="list-group-item" href="./guide-structure-widgets.html">Widgets</a>
<a class="list-group-item" href="./guide-structure-assets.html">Assets</a>
<a class="list-group-item" href="./guide-structure-extensions.html">Extensions</a></div>
<a class="list-group-item active" href="#navigation-1276" data-toggle="collapse" data-parent="#navigation">Handling Requests <b class="caret"></b></a><div id="navigation-1276" class="submenu panel-collapse collapse in"><a class="list-group-item" href="./guide-runtime-overview.html">Overview</a>
<a class="list-group-item" href="./guide-runtime-bootstrapping.html">Bootstrapping</a>
<a class="list-group-item" href="./guide-runtime-routing.html">Routing and URL Creation</a>
<a class="list-group-item" href="./guide-runtime-requests.html">Requests</a>
<a class="list-group-item" href="./guide-runtime-responses.html">Responses</a>
<a class="list-group-item" href="./guide-runtime-sessions-cookies.html">Sessions and Cookies</a>
<a class="list-group-item active" href="./guide-runtime-handling-errors.html">Handling Errors</a>
<a class="list-group-item" href="./guide-runtime-logging.html">Logging</a></div>
<a class="list-group-item" href="#navigation-1277" data-toggle="collapse" data-parent="#navigation">Key Concepts <b class="caret"></b></a><div id="navigation-1277" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-concept-components.html">Components</a>
<a class="list-group-item" href="./guide-concept-properties.html">Properties</a>
<a class="list-group-item" href="./guide-concept-events.html">Events</a>
<a class="list-group-item" href="./guide-concept-behaviors.html">Behaviors</a>
<a class="list-group-item" href="./guide-concept-configurations.html">Configurations</a>
<a class="list-group-item" href="./guide-concept-aliases.html">Aliases</a>
<a class="list-group-item" href="./guide-concept-autoloading.html">Class Autoloading</a>
<a class="list-group-item" href="./guide-concept-service-locator.html">Service Locator</a>
<a class="list-group-item" href="./guide-concept-di-container.html">Dependency Injection Container</a></div>
<a class="list-group-item" href="#navigation-1278" data-toggle="collapse" data-parent="#navigation">Working with Databases <b class="caret"></b></a><div id="navigation-1278" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-db-dao.html">Data Access Objects</a>
<a class="list-group-item" href="./guide-db-query-builder.html">Query Builder</a>
<a class="list-group-item" href="./guide-db-active-record.html">Active Record</a>
<a class="list-group-item" href="./guide-db-migrations.html">Migrations</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-sphinx/blob/master/docs/guide/README.md">Sphinx</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-redis/blob/master/docs/guide/README.md">Redis</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-mongodb/blob/master/docs/guide/README.md">MongoDB</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-elasticsearch/blob/master/docs/guide/README.md">ElasticSearch</a></div>
<a class="list-group-item" href="#navigation-1279" data-toggle="collapse" data-parent="#navigation">Getting Data from Users <b class="caret"></b></a><div id="navigation-1279" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-input-forms.html">Creating Forms</a>
<a class="list-group-item" href="./guide-input-validation.html">Validating Input</a>
<a class="list-group-item" href="./guide-input-file-upload.html">Uploading Files</a>
<a class="list-group-item" href="./guide-input-tabular-input.html">Collecting Tabular Input</a>
<a class="list-group-item" href="./guide-input-multiple-models.html">Getting Data for Multiple Models</a></div>
<a class="list-group-item" href="#navigation-1280" data-toggle="collapse" data-parent="#navigation">Displaying Data <b class="caret"></b></a><div id="navigation-1280" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-output-formatting.html">Data Formatting</a>
<a class="list-group-item" href="./guide-output-pagination.html">Pagination</a>
<a class="list-group-item" href="./guide-output-sorting.html">Sorting</a>
<a class="list-group-item" href="./guide-output-data-providers.html">Data Providers</a>
<a class="list-group-item" href="./guide-output-data-widgets.html">Data Widgets</a>
<a class="list-group-item" href="./guide-output-client-scripts.html">Working with Client Scripts</a>
<a class="list-group-item" href="./guide-output-theming.html">Theming</a></div>
<a class="list-group-item" href="#navigation-1281" data-toggle="collapse" data-parent="#navigation">Security <b class="caret"></b></a><div id="navigation-1281" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-security-overview.html">Overview</a>
<a class="list-group-item" href="./guide-security-authentication.html">Authentication</a>
<a class="list-group-item" href="./guide-security-authorization.html">Authorization</a>
<a class="list-group-item" href="./guide-security-passwords.html">Working with Passwords</a>
<a class="list-group-item" href="./guide-security-cryptography.html">Cryptography</a>
<a class="list-group-item" href="./guide-structure-views.html#security">Views security</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-authclient/blob/master/docs/guide/README.md">Auth Clients</a>
<a class="list-group-item" href="./guide-security-best-practices.html">Best Practices</a></div>
<a class="list-group-item" href="#navigation-1282" data-toggle="collapse" data-parent="#navigation">Caching <b class="caret"></b></a><div id="navigation-1282" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-caching-overview.html">Overview</a>
<a class="list-group-item" href="./guide-caching-data.html">Data Caching</a>
<a class="list-group-item" href="./guide-caching-fragment.html">Fragment Caching</a>
<a class="list-group-item" href="./guide-caching-page.html">Page Caching</a>
<a class="list-group-item" href="./guide-caching-http.html">HTTP Caching</a></div>
<a class="list-group-item" href="#navigation-1283" data-toggle="collapse" data-parent="#navigation">RESTful Web Services <b class="caret"></b></a><div id="navigation-1283" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-rest-quick-start.html">Quick Start</a>
<a class="list-group-item" href="./guide-rest-resources.html">Resources</a>
<a class="list-group-item" href="./guide-rest-controllers.html">Controllers</a>
<a class="list-group-item" href="./guide-rest-routing.html">Routing</a>
<a class="list-group-item" href="./guide-rest-response-formatting.html">Response Formatting</a>
<a class="list-group-item" href="./guide-rest-authentication.html">Authentication</a>
<a class="list-group-item" href="./guide-rest-rate-limiting.html">Rate Limiting</a>
<a class="list-group-item" href="./guide-rest-versioning.html">Versioning</a>
<a class="list-group-item" href="./guide-rest-error-handling.html">Error Handling</a></div>
<a class="list-group-item" href="#navigation-1284" data-toggle="collapse" data-parent="#navigation">Development Tools <b class="caret"></b></a><div id="navigation-1284" class="submenu panel-collapse collapse"><a class="list-group-item" href="https://github.com/yiisoft/yii2-debug/blob/master/docs/guide/README.md">Debug Toolbar and Debugger</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-gii/blob/master/docs/guide/README.md">Generating Code using Gii</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-apidoc">Generating API Documentation</a></div>
<a class="list-group-item" href="#navigation-1285" data-toggle="collapse" data-parent="#navigation">Testing <b class="caret"></b></a><div id="navigation-1285" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-test-overview.html">Overview</a>
<a class="list-group-item" href="./guide-test-environment-setup.html">Testing environment setup</a>
<a class="list-group-item" href="./guide-test-unit.html">Unit Tests</a>
<a class="list-group-item" href="./guide-test-functional.html">Functional Tests</a>
<a class="list-group-item" href="./guide-test-acceptance.html">Acceptance Tests</a>
<a class="list-group-item" href="./guide-test-fixtures.html">Fixtures</a></div>
<a class="list-group-item" href="#navigation-1286" data-toggle="collapse" data-parent="#navigation">Special Topics <b class="caret"></b></a><div id="navigation-1286" class="submenu panel-collapse collapse"><a class="list-group-item" href="https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/README.md">Advanced Project Template</a>
<a class="list-group-item" href="./guide-tutorial-start-from-scratch.html">Building Application from Scratch</a>
<a class="list-group-item" href="./guide-tutorial-console.html">Console Commands</a>
<a class="list-group-item" href="./guide-tutorial-core-validators.html">Core Validators</a>
<a class="list-group-item" href="./guide-tutorial-i18n.html">Internationalization</a>
<a class="list-group-item" href="./guide-tutorial-mailing.html">Mailing</a>
<a class="list-group-item" href="./guide-tutorial-performance-tuning.html">Performance Tuning</a>
<a class="list-group-item" href="./guide-tutorial-shared-hosting.html">Shared Hosting Environment</a>
<a class="list-group-item" href="./guide-tutorial-template-engines.html">Template Engines</a>
<a class="list-group-item" href="./guide-tutorial-yii-integration.html">Working with Third-Party Code</a></div>
<a class="list-group-item" href="#navigation-1287" data-toggle="collapse" data-parent="#navigation">Widgets <b class="caret"></b></a><div id="navigation-1287" class="submenu panel-collapse collapse"><a class="list-group-item" href="https://github.com/yiisoft/yii2-bootstrap/blob/master/docs/guide/README.md">Bootstrap Widgets</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-jui/blob/master/docs/guide/README.md">jQuery UI Widgets</a></div>
<a class="list-group-item" href="#navigation-1288" data-toggle="collapse" data-parent="#navigation">Helpers <b class="caret"></b></a><div id="navigation-1288" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-helper-overview.html">Overview</a>
<a class="list-group-item" href="./guide-helper-array.html">ArrayHelper</a>
<a class="list-group-item" href="./guide-helper-html.html">Html</a>
<a class="list-group-item" href="./guide-helper-url.html">Url</a></div></div>    </div>
    <div class="col-md-9 guide-content" role="main">
        <h1>Handling Errors <span id="handling-errors"></span><a href="#handling-errors" class="hashlink">&para;</a></h1>
<div class="toc"><ol><li><a href="#using-error-handler">Using Error Handler</a></li>
<li><a href="#customizing-error-display">Customizing Error Display</a></li></ol></div>
<p>Yii includes a built-in <a href="./yii-web-errorhandler.html">error handler</a> which makes error handling a much more pleasant
experience than before. In particular, the Yii error handler does the following to improve error handling:</p>
<ul>
<li>All non-fatal PHP errors (e.g. warnings, notices) are converted into catchable exceptions.</li>
<li>Exceptions and fatal PHP errors are displayed with detailed call stack information and source code lines
in debug mode.</li>
<li>Supports using a dedicated <a href="guide-structure-controllers.html#actions">controller action</a> to display errors.</li>
<li>Supports different error response formats.</li>
</ul>
<p>The <a href="./yii-web-errorhandler.html">error handler</a> is enabled by default. You may disable it by defining the constant
<code>YII_ENABLE_ERROR_HANDLER</code> to be false in the <a href="guide-structure-entry-scripts.html">entry script</a> of your application.</p>
<h2>Using Error Handler  <span id="using-error-handler"></span><a href="#using-error-handler" class="hashlink">&para;</a></h2><p>The <a href="./yii-web-errorhandler.html">error handler</a> is registered as an <a href="guide-structure-application-components.html">application component</a> named <code>errorHandler</code>.
You may configure it in the application configuration like the following:</p>
<pre><code class="hljs php language-php"><span class="hljs-keyword">return</span> [
    <span class="hljs-string">'components'</span> =&gt; [
        <span class="hljs-string">'errorHandler'</span> =&gt; [
            <span class="hljs-string">'maxSourceLines'</span> =&gt; <span class="hljs-number">20</span>,
        ],
    ],
];
</code></pre>
<p>With the above configuration, the number of source code lines to be displayed in exception pages will be up to 20.</p>
<p>As aforementioned, the error handler turns all non-fatal PHP errors into catchable exceptions. This means you can
use the following code to deal with PHP errors:</p>
<pre><code class="hljs php language-php"><span class="hljs-keyword">use</span> <span class="hljs-title">Yii</span>;
<span class="hljs-keyword">use</span> <span class="hljs-title">yii</span>\<span class="hljs-title">base</span>\<span class="hljs-title">ErrorException</span>;

<span class="hljs-keyword">try</span> {
    <span class="hljs-number">10</span>/<span class="hljs-number">0</span>;
} <span class="hljs-keyword">catch</span> (ErrorException <span class="hljs-variable">$e</span>) {
    Yii::warning(<span class="hljs-string">"Division by zero."</span>);
}

<span class="hljs-comment">// execution continues...</span>
</code></pre>
<p>If you want to show an error page telling the user that his request is invalid or unexpected, you may simply
throw an <a href="./yii-web-httpexception.html">HTTP exception</a>, such as <a href="./yii-web-notfoundhttpexception.html">yii\web\NotFoundHttpException</a>. The error handler
will correctly set the HTTP status code of the response and use an appropriate error view to display the error
message.</p>
<pre><code class="hljs php language-php"><span class="hljs-keyword">use</span> <span class="hljs-title">yii</span>\<span class="hljs-title">web</span>\<span class="hljs-title">NotFoundHttpException</span>;

<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> NotFoundHttpException();
</code></pre>
<h2>Customizing Error Display  <span id="customizing-error-display"></span><a href="#customizing-error-display" class="hashlink">&para;</a></h2><p>The <a href="./yii-web-errorhandler.html">error handler</a> adjusts the error display according to the value of the constant <code>YII_DEBUG</code>.
When <code>YII_DEBUG</code> is true (meaning in debug mode), the error handler will display exceptions with detailed call
stack information and source code lines to help easier debugging. And when <code>YII_DEBUG</code> is false, only the error
message will be displayed to prevent revealing sensitive information about the application.</p>
<blockquote class="info"><p><strong>Info: </strong>If an exception is a descendant of <a href="./yii-base-userexception.html">yii\base\UserException</a>, no call stack will be displayed regardless
the value of <code>YII_DEBUG</code>. This is because such exceptions are considered to be caused by user mistakes and the
developers do not need to fix anything.</p>
</blockquote>
<p>By default, the <a href="./yii-web-errorhandler.html">error handler</a> displays errors using two <a href="guide-structure-views.html">views</a>:</p>
<ul>
<li><code>@yii/views/errorHandler/error.php</code>: used when errors should be displayed WITHOUT call stack information.
When <code>YII_DEBUG</code> is false, this is the only error view to be displayed.</li>
<li><code>@yii/views/errorHandler/exception.php</code>: used when errors should be displayed WITH call stack information.</li>
</ul>
<p>You can configure the <a href="./yii-web-errorhandler.html#$errorView-detail">errorView</a> and <a href="./yii-web-errorhandler.html#$exceptionView-detail">exceptionView</a>
properties of the error handler to use your own views to customize the error display.</p>
<h3>Using Error Actions  <span id="using-error-actions"></span><a href="#using-error-actions" class="hashlink">&para;</a></h3><p>A better way of customizing the error display is to use dedicated error <a href="guide-structure-controllers.html">actions</a>.
To do so, first configure the <a href="./yii-web-errorhandler.html#$errorAction-detail">errorAction</a> property of the <code>errorHandler</code>
component like the following:</p>
<pre><code class="hljs php language-php"><span class="hljs-keyword">return</span> [
    <span class="hljs-string">'components'</span> =&gt; [
        <span class="hljs-string">'errorHandler'</span> =&gt; [
            <span class="hljs-string">'errorAction'</span> =&gt; <span class="hljs-string">'site/error'</span>,
        ],
    ]
];
</code></pre>
<p>The <a href="./yii-web-errorhandler.html#$errorAction-detail">errorAction</a> property takes a <a href="guide-structure-controllers.html#routes">route</a>
to an action. The above configuration states that when an error needs to be displayed without call stack information,
the <code>site/error</code> action should be executed.</p>
<p>You can create the <code>site/error</code> action as follows,</p>
<pre><code class="hljs php language-php"><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">controllers</span>;

<span class="hljs-keyword">use</span> <span class="hljs-title">Yii</span>;
<span class="hljs-keyword">use</span> <span class="hljs-title">yii</span>\<span class="hljs-title">web</span>\<span class="hljs-title">Controller</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SiteController</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Controller</span>
</span>{
    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">actions</span><span class="hljs-params">()</span>
    </span>{
        <span class="hljs-keyword">return</span> [
            <span class="hljs-string">'error'</span> =&gt; [
                <span class="hljs-string">'class'</span> =&gt; <span class="hljs-string">'yii\web\ErrorAction'</span>,
            ],
        ];
    }
}
</code></pre>
<p>The above code defines the <code>error</code> action using the <a href="./yii-web-erroraction.html">yii\web\ErrorAction</a> class which renders an error
using a view named <code>error</code>.</p>
<p>Besides using <a href="./yii-web-erroraction.html">yii\web\ErrorAction</a>, you may also define the <code>error</code> action using an action method like the following,</p>
<pre><code class="hljs php language-php"><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">actionError</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-variable">$exception</span> = Yii::<span class="hljs-variable">$app</span>-&gt;errorHandler-&gt;exception;
    <span class="hljs-keyword">if</span> (<span class="hljs-variable">$exception</span> !== <span class="hljs-keyword">null</span>) {
        <span class="hljs-keyword">return</span> <span class="hljs-variable">$this</span>-&gt;render(<span class="hljs-string">'error'</span>, [<span class="hljs-string">'exception'</span> =&gt; <span class="hljs-variable">$exception</span>]);
    }
}
</code></pre>
<p>You should now create a view file located at <code>views/site/error.php</code>. In this view file, you can access
the following variables if the error action is defined as <a href="./yii-web-erroraction.html">yii\web\ErrorAction</a>:</p>
<ul>
<li><code>name</code>: the name of the error;</li>
<li><code>message</code>: the error message;</li>
<li><code>exception</code>: the exception object through which you can retrieve more useful information, such as HTTP status code,
error code, error call stack, etc.</li>
</ul>
<blockquote class="info"><p><strong>Info: </strong>If you are using the <a href="guide-start-installation.html">basic project template</a> or the <a href="https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/README.md">advanced project template</a>,
the error action and the error view are already defined for you.</p>
</blockquote>
<blockquote class="note"><p><strong>Note: </strong>If you need to redirect in an error handler, do it the following way:
<code>`</code>php
Yii::$app-&gt;getResponse()-&gt;redirect($url)-&gt;send();
return;
<code>`</code></p>
</blockquote>
<h3>Customizing Error Response Format  <span id="error-format"></span><a href="#error-format" class="hashlink">&para;</a></h3><p>The error handler displays errors according to the format setting of the <a href="guide-runtime-responses.html">response</a>.
If the the <a href="./yii-web-response.html#$format-detail">response format</a> is <code>html</code>, it will use the error or exception view
to display errors, as described in the last subsection. For other response formats, the error handler will
assign the array representation of the exception to the <a href="./yii-web-response.html#$data-detail">yii\web\Response::$data</a> property which will then
be converted to different formats accordingly. For example, if the response format is <code>json</code>, you may see
the following response:</p>
<pre><code class="hljs http"><span class="hljs-status">HTTP/1.1 <span class="hljs-number">404</span> Not Found</span>
<span class="hljs-attribute">Date</span>: <span class="hljs-string">Sun, 02 Mar 2014 05:31:43 GMT</span>
<span class="hljs-attribute">Server</span>: <span class="hljs-string">Apache/2.2.26 (Unix) DAV/2 PHP/5.4.20 mod_ssl/2.2.26 OpenSSL/0.9.8y</span>
<span class="hljs-attribute">Transfer-Encoding</span>: <span class="hljs-string">chunked</span>
<span class="hljs-attribute">Content-Type</span>: <span class="hljs-string">application/json; charset=UTF-8</span>

<span class="json">{
    "<span class="hljs-attribute">name</span>": <span class="hljs-value"><span class="hljs-string">"Not Found Exception"</span></span>,
    "<span class="hljs-attribute">message</span>": <span class="hljs-value"><span class="hljs-string">"The requested resource was not found."</span></span>,
    "<span class="hljs-attribute">code</span>": <span class="hljs-value"><span class="hljs-number">0</span></span>,
    "<span class="hljs-attribute">status</span>": <span class="hljs-value"><span class="hljs-number">404</span>
</span>}
</span></code></pre>
<p>You may customize the error response format by responding to the <code>beforeSend</code> event of the <code>response</code> component
in the application configuration:</p>
<pre><code class="hljs php language-php"><span class="hljs-keyword">return</span> [
    <span class="hljs-comment">// ...</span>
    <span class="hljs-string">'components'</span> =&gt; [
        <span class="hljs-string">'response'</span> =&gt; [
            <span class="hljs-string">'class'</span> =&gt; <span class="hljs-string">'yii\web\Response'</span>,
            <span class="hljs-string">'on beforeSend'</span> =&gt; <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(<span class="hljs-variable">$event</span>)</span> </span>{
                <span class="hljs-variable">$response</span> = <span class="hljs-variable">$event</span>-&gt;sender;
                <span class="hljs-keyword">if</span> (<span class="hljs-variable">$response</span>-&gt;data !== <span class="hljs-keyword">null</span>) {
                    <span class="hljs-variable">$response</span>-&gt;data = [
                        <span class="hljs-string">'success'</span> =&gt; <span class="hljs-variable">$response</span>-&gt;isSuccessful,
                        <span class="hljs-string">'data'</span> =&gt; <span class="hljs-variable">$response</span>-&gt;data,
                    ];
                    <span class="hljs-variable">$response</span>-&gt;statusCode = <span class="hljs-number">200</span>;
                }
            },
        ],
    ],
];
</code></pre>
<p>The above code will reformat the error response like the following:</p>
<pre><code class="hljs http"><span class="hljs-status">HTTP/1.1 <span class="hljs-number">200</span> OK</span>
<span class="hljs-attribute">Date</span>: <span class="hljs-string">Sun, 02 Mar 2014 05:31:43 GMT</span>
<span class="hljs-attribute">Server</span>: <span class="hljs-string">Apache/2.2.26 (Unix) DAV/2 PHP/5.4.20 mod_ssl/2.2.26 OpenSSL/0.9.8y</span>
<span class="hljs-attribute">Transfer-Encoding</span>: <span class="hljs-string">chunked</span>
<span class="hljs-attribute">Content-Type</span>: <span class="hljs-string">application/json; charset=UTF-8</span>

<span class="json">{
    "<span class="hljs-attribute">success</span>": <span class="hljs-value"><span class="hljs-literal">false</span></span>,
    "<span class="hljs-attribute">data</span>": <span class="hljs-value">{
        "<span class="hljs-attribute">name</span>": <span class="hljs-value"><span class="hljs-string">"Not Found Exception"</span></span>,
        "<span class="hljs-attribute">message</span>": <span class="hljs-value"><span class="hljs-string">"The requested resource was not found."</span></span>,
        "<span class="hljs-attribute">code</span>": <span class="hljs-value"><span class="hljs-number">0</span></span>,
        "<span class="hljs-attribute">status</span>": <span class="hljs-value"><span class="hljs-number">404</span>
    </span>}
</span>}
</span></code></pre>
        <div class="toplink"><a href="#" class="h1" title="go to top"><span class="glyphicon glyphicon-arrow-up"></a></div>
    </div>
</div>


</div>

<footer class="footer">
        <p class="pull-right"><small>Page generated on Sat, 09 Jul 2016 12:16:30 +0000</small></p>
    Powered by <a href="http://www.yiiframework.com/" rel="external">Yii Framework</a></footer>

<script type="text/javascript">jQuery(document).ready(function () {
    var shiftWindow = function () { scrollBy(0, -50) };
    if (location.hash) setTimeout(shiftWindow, 1);
    window.addEventListener("hashchange", shiftWindow);
var element = document.createElement("script");
element.src = "./jssearch.index.js";
document.body.appendChild(element);

var searchBox = $('#searchbox');

// search when typing in search field
searchBox.on("keyup", function(event) {
    var query = $(this).val();

    if (query == '' || event.which == 27) {
        $('#search-resultbox').hide();
        return;
    } else if (event.which == 13) {
        var selectedLink = $('#search-resultbox a.selected');
        if (selectedLink.length != 0) {
            document.location = selectedLink.attr('href');
            return;
        }
    } else if (event.which == 38 || event.which == 40) {
        $('#search-resultbox').show();

        var selected = $('#search-resultbox a.selected');
        if (selected.length == 0) {
            $('#search-results').find('a').first().addClass('selected');
        } else {
            var next;
            if (event.which == 40) {
                next = selected.parent().next().find('a').first();
            } else {
                next = selected.parent().prev().find('a').first();
            }
            if (next.length != 0) {
                var resultbox = $('#search-results');
                var position = next.position();

//              TODO scrolling is buggy and jumps around
//                resultbox.scrollTop(Math.floor(position.top));
//                console.log(position.top);

                selected.removeClass('selected');
                next.addClass('selected');
            }
        }

        return;
    }
    $('#search-resultbox').show();
    $('#search-results').html('<li><span class="no-results">No results</span></li>');

    var result = jssearch.search(query);

    if (result.length > 0) {
        var i = 0;
        var resHtml = '';

        for (var key in result) {
            if (i++ > 20) {
                break;
            }
            resHtml = resHtml +
            '<li><a href="' + result[key].file.u.substr(3) +'"><span class="title">' + result[key].file.t + '</span>' +
            '<span class="description">' + result[key].file.d + '</span></a></li>';
        }
        $('#search-results').html(resHtml);
    }
});

// hide the search results on ESC
$(document).on("keyup", function(event) { if (event.which == 27) { $('#search-resultbox').hide(); } });
// hide search results on click to document
$(document).bind('click', function (e) { $('#search-resultbox').hide(); });
// except the following:
searchBox.bind('click', function(e) { e.stopPropagation(); });
$('#search-resultbox').bind('click', function(e) { e.stopPropagation(); });

});</script></body>
</html>
